package com.yueguang.springai.factory;

import com.yueguang.springai.handle.NodeHandler;
import com.yueguang.springai.handle.OrderHandlerEnum;
import com.yueguang.springai.model.dto.OrderDO;
import com.yueguang.springai.model.vo.OrderCreateRequestVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class OrderFactory {

    @Resource
    private Map<String, NodeHandler> nodeHandlerMap;

    /**
     * 创建订单
     * @param requestVO 订单参数
     * @return 订单实体DO
     */
    public OrderDO createOrder(OrderCreateRequestVO requestVO){
        final Integer orderType = requestVO.getOrderType();
        // 获取node规则执行器名称
        final String ruleHandler = OrderHandlerEnum.getRuleHandler(orderType);
        System.out.println("ruleHandler="+ruleHandler);
        // 获取node规则执行器
        System.out.println("nodeHandlerMap="+nodeHandlerMap);
        final NodeHandler nodeHandler = nodeHandlerMap.get(ruleHandler);
        System.out.println("nodeHandler="+nodeHandler);
        if (nodeHandler == null){
            // 异常
            throw new RuntimeException();
        }
        return nodeHandler.handleOrder(requestVO);
    }
}
